<template>
	<view class="navbar">
		<view class="navbar-fixed">
			<!-- #ifndef MP-ALIPAY -->
			<view :style="{height:statusBar+'px'}"></view>
			<!-- #endif -->
			<view class="navbar-content" :class="{search:isSearch}" :style="{height:HeaderBar+'px',width:serachWidth+'px'}" @click.stop="open">
				<view v-if="isSearch" class="navbar-content__search-icons" @click="back">
					<uni-icons type="back" size="22" color="#fff"></uni-icons>
				</view>
				<view v-if="!isSearch" class="navbar-search">
					<view class="navbar-search-icon">
						<uni-icons type="search" size="22" color="#999"/>
					</view>
					<view class="navbar-search-text">uni-app、Vue</view>
				</view>
				<view v-else class="navbar-search">
					<input class="navbar-search-text" type="text" v-model="val" placeholder="请输入亲要搜索的内容" @input="inputChange" />
				</view>
			</view>
		</view>
		<view :style="{height:CustomBar+'px'}"></view>
	</view>
</template>

<script>
	export default {
		name: "navbar",
		props: {
			value : {
				type:[String,Number],
				default:'',
			},
			isSearch: {
				type: Boolean,
				default: false,
			},
		},
		data() {
			return {
				HeaderBar: 45,
				statusBar: 20,
				serachWidth: '',
				CustomBar:45,
				val : '',
			};
		},
		watch: {
			value(newValue, oldValue) {
				this.val = newValue
			}
		},
		created() {
			uni.getSystemInfo().then(res => {
				const [error, data] = res;
				this.statusBar = data.statusBarHeight;
				this.serachWidth = data.windowWidth;
				//#ifndef H5 || APP-PLUS || MP-ALIPAY
				const rect = uni.getMenuButtonBoundingClientRect();
				if (data.system.toLowerCase().indexOf('ios') > -1) {
					//ios
					this.CustomBar = rect.bottom + (rect.top - data.statusBarHeight) * 2;
					this.HeaderBar = this.CustomBar - data.statusBarHeight;
					this.serachWidth = rect.left;
					console.log(this.CustomBar, this.HeaderBar, this.statusBar, rect.left);
				} else {
					//安卓  
					this.HeaderBar = rect.height + (rect.top - data.statusBarHeight) * 2;
					this.CustomBar = this.HeaderBar + data.statusBarHeight;
					this.serachWidth = rect.left;
					console.log(this.CustomBar, this.HeaderBar, this.statusBar, rect.left);
				}
				//#endif
			});
		},
		methods: {
			open() {
				if (this.isSearch) {
					return
				}
				uni.navigateTo({
					url:"/pages/home-search/home-search"
				})
			},
			inputChange(e){
				const {value} = e.detail;
				this.$emit('input',value)
			},
			back(){
				uni.switchTab({
					url:'/pages/tabbar/index/index'	
				})
			}
		},
	}
</script>

<style lang="scss">
	.navbar {
		.navbar-fixed{
			position: fixed;
			top: var(--window-top);
			left: 0;
			z-index: 99;
			// display: flex;
			// flex-direction: column;
			width: 750rpx;
			background-color: $mk-base-color;
			
			.navbar-content {
				display: flex;
				justify-content: center;
				align-items: center;
				padding: 0 15px;
				// height: 45px;
				box-sizing: border-box;
				
				.navbar-search {
					display: flex;
					align-items: center;
					border-radius: 30px;
					padding: 0 10px;
					width: 100%;
					height: 32px;
					// line-height: 32px;
					background-color: #FFFFFF;
			
					.navbar-search-icon {
						// width: 10px;
						// height: 10px;
						margin-bottom: -2.5px;
						vertical-align:middle;
						// display: inline-block;
						// border: 1px red solid;
						margin-right: 10px;
					}
			
					.navbar-search-text {
						vertical-align:middle;
						width: 100%;
						// display: inline-block;
						font-size: 14px;
						color: #999999;
						// line-height: 30px;
					}
					
				}
				
				&.search{
					padding-left: 0;
					.navbar-content__search-icons{
						margin-left: 10px;
						margin-right: 10px;
					}
					.navbar-search{
						border-radius: 5px;
					}
				}
			}
		}
	}
</style>
